package Chapter20.tankgame5;

import java.io.*;
import java.util.Vector;

/**
 * @author 杨森
 * @date 2022年11月06日 10:07
 * 该类用于记录相关的信息和文件交互
 */
public class Recorder {
    //定义变量，记录我放击毁敌人坦克数
    private static int allEnemyTankNum = 0;
    //    定义IO对象
    private static FileWriter fw = null;
    private static BufferedWriter bw = null;
    private static BufferedReader br = null;
    private static String recordFile = "src\\myRecord.txt";
    //    定义Vector，指向MyPanel对象得到敌人坦克Vector
    private static Vector<EnemyTank> enemyTanks = null;
    //    定义一个Node的Vector 用于保存敌人的信息node
    private static Vector<Node> nodes = new Vector<>();

    public static String getRecordFile() {
        return recordFile;
    }

    public static void setEnemyTanks(Vector<EnemyTank> enemyTanks) {
        Recorder.enemyTanks = enemyTanks;
    }

    //    增加一个方法，用于读取recorderFile 回复相关信息
//    该方法在继续上局的时候启动
    public static Vector<Node> getNodeAndEnemyTanksRec() {
        try {
            br = new BufferedReader(new FileReader(recordFile));
            allEnemyTankNum = Integer.parseInt(br.readLine());
//            循环读取文件，生成nodes集合
            String line = "";
            while ((line = br.readLine()) != null) {
                String[] xyd=line.split(" ");
                Node node = new Node(Integer.parseInt(xyd[0]), Integer.parseInt(xyd[1]), Integer.parseInt(xyd[2]));
                nodes.add(node);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return nodes;
    }

    //    增加一个方法，当游戏退出时，就将allEnemyTankNum保存到recordFile
//    保存敌人坦克的坐标和方向
    public static void keepRecord() {
        try {
            bw = new BufferedWriter(new FileWriter(recordFile));
            bw.write(allEnemyTankNum + "\r\n");
//            遍历敌人坦克的Vector，根据情况保存
            for (int i = 0; i < enemyTanks.size(); i++) {
//                取出敌人坦克
                EnemyTank enemyTank = enemyTanks.get(i);
                if (enemyTank.isLive) {
//                    保存该EnemyTank信息
                    String records = enemyTank.getX() + " " + enemyTank.getY() + " " + enemyTank.getDirect() + " ";
//                    写入文件
                    bw.write(records + "\r\n");
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static int getAllEnemyTankNum() {
        return allEnemyTankNum;
    }

    public static void setAllEnemyTankNum(int allEnemyTankNum) {
        Recorder.allEnemyTankNum = allEnemyTankNum;
    }

    //  当我方坦克击毁一个敌人坦克，就应当allEnemyTankNum++
    public static void addAllEnemyTankNum() {
        Recorder.allEnemyTankNum++;
    }

}
